package com.boot.test;

import io.reactivex.Observable;

import java.util.function.Supplier;

public class RxJavaDemo1 {
    public static void main(String[] args){
//        Observable.just("hello world").subscribe(System.out::println,
//                System.err::println,
//                () -> System.out.println("complete"),
//                (disposable)->System.out.println("subscribe"));

        Observable.just("Hello world!")
        //发射数据前调用，一般用于subscribe之前对数据进行处理。
        .doOnNext(s -> System.out.println("doOnNext:" + s))
        //发射数据之后调用
        //.doAfterNext(s -> System.out.println("doAfterNext: " + s))
        .doOnComplete(() -> System.out.println("doOnComplete: "))
        //一旦观察者订阅了就会被调用
        .doOnSubscribe(disposable -> System.out.println("doOnSubscribe: "))
        //当调用onComplete或onError时触发
        .doAfterTerminate(() -> System.out.println("doAfterTerminate: "))
        //无论正常终止还是异常终止，doFinally先于doAfterTerminal调用
        // .doFinally(() -> System.out.println("doFinally: "))
        //每发射一个数据就会触发，不仅onNext,还包括onError和onComplete
                .doOnEach(stringNotification -> System.out.println("doOnEach: " + (stringNotification.isOnNext() ? "onNext"
                        : (stringNotification.isOnComplete() ? "onComplete" : "onError"))))
        //订阅后可以取消订阅
                .doOnLifecycle(disposable -> System.out.println("doOnLifecycle: " + disposable.isDisposed()),
                        () -> System.out.println("doOnLifecycle run: "))
        .subscribe(System.out::println, System.err::println, () -> System.out.println("complete"),
                        (disposable) -> System.out.println("subscribe"));
        Supplier<SteamTest> a = SteamTest::new;
        System.out.println("23"+a.get());
        System.out.println("12"+a.get());
    }
}
